﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class ZL_WL_Query : BasePage
    {
        ZL_WL_Dao dao = new ZL_WL_Dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "物料列表";
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
            List<CS_WLLX> lsWLLX = daoWLLX.QueryAll();
            this.ddlLXBH.DataSource = lsWLLX;
            this.ddlLXBH.DataTextField = "MC";
            this.ddlLXBH.DataValueField = "BH";
            this.ddlLXBH.DataBind();
            this.ddlLXBH.Items.Insert(0, new ListItem("请选择", "0"));

            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            List<ZL_GYS> lsGYS = daoGYS.QueryAll();
            this.ddlGYSID.DataSource = lsGYS;
            this.ddlGYSID.DataValueField = "ID";
            this.ddlGYSID.DataTextField = "MC";
            this.ddlGYSID.DataBind();
            this.ddlGYSID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {

            // 产品单位
            CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsDW = daoDW.Query4WL();

            CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
            List<CS_WLLX> lsWLLX = daoWLLX.QueryAll();

            ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            List<ZL_GYS> lsGYS = daoGYS.QueryAll();

            GridView1.PageIndex = RestorePageIndex();
            List<ZL_WL> listWL = query(); //  dao.QueryAll();
            List<ZL_WL_Ex> listEx = new List<ZL_WL_Ex>();

            ZL_WL_Ex ce;
            foreach (ZL_WL c in listWL)
            {
                ce = new ZL_WL_Ex();
                ce.ID = c.ID.ToString();
                ce.BH = c.BH;
                ce.MC = c.MC;
                ce.GG = c.GG;

                CS_JBDW dw = lsDW.Find(delegate(CS_JBDW p) { return p.BH == c.DWBH; });
                ce.DW = dw == null ? "" : dw.MC;
                CS_WLLX lx = lsWLLX.Find(delegate(CS_WLLX p) { return p.BH == c.LXBH; });
                ce.LX = lx == null ? "" : lx.MC;
                ZL_GYS gys = lsGYS.Find(delegate(ZL_GYS p) { return p.ID == c.GYSID; });
                ce.GYS = gys == null ? "" : gys.MC;
                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected class ZL_WL_Ex
        {
            public string ID { get; set; }
            public string BH { get; set; }
            public string MC { get; set; }
            public string DW { get; set; }
            public string LX { get; set; }
            public string GG { get; set; }
            public string GYS { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_WL_AddEx.aspx");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();

            //string bh = this.tbBH.Text;
            //string mc = this.tbMC.Text;
            //string lxbh = this.ddlLXBH.SelectedValue;
            //string gysId = this.ddlGYSID.SelectedValue;

            //List<ZL_WL> listWL = query();

            //// 产品单位
            //CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            //List<CS_JBDW> lsDW = daoDW.Query4WL();

            //CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
            //List<CS_WLLX> lsWLLX = daoWLLX.QueryAll();

            //ZL_GYS_Dao daoGYS = new ZL_GYS_Dao();
            //List<ZL_GYS> lsGYS = daoGYS.QueryAll();

            //List<ZL_WL_Ex> listEx = new List<ZL_WL_Ex>();

            //ZL_WL_Ex ce;
            //foreach (ZL_WL c in listWL)
            //{
            //    ce = new ZL_WL_Ex();
            //    ce.ID = c.ID.ToString();
            //    ce.BH = c.BH;
            //    ce.MC = c.MC;
            //    ce.GG = c.GG;

            //    CS_JBDW dw = lsDW.Find(delegate(CS_JBDW p) { return p.BH == c.DWBH; });
            //    ce.DW = dw == null ? "" : dw.MC;
            //    CS_WLLX lx = lsWLLX.Find(delegate(CS_WLLX p) { return p.BH == c.LXBH; });
            //    ce.LX = lx == null ? "" : lx.MC;
            //    ZL_GYS gys = lsGYS.Find(delegate(ZL_GYS p) { return p.ID == c.GYSID; });
            //    ce.GYS = gys == null ? "" : gys.MC;
            //    listEx.Add(ce);
            //}

            //this.GridView1.DataSource = listWL;
            //this.GridView1.DataBind();
        }

        protected List<ZL_WL> query()
        {
            string bh = this.tbBH.Text;
            string mc = this.tbMC.Text;
            string lxbh = this.ddlLXBH.SelectedValue;
            string gysId = this.ddlGYSID.SelectedValue;

            return dao.QueryBySql(bh, mc, lxbh, gysId);
        }
    }
}